package com.yang.service.adminimpl;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yang.bean.*;
import com.yang.bean.common.BaseRespVo;
import com.yang.bean.common.CommonData;
import com.yang.bean.yvo.*;
import com.yang.mapper.MarketCommentMapper;
import com.yang.mapper.MarketOrderGoodsMapper;
import com.yang.mapper.MarketOrderMapper;
import com.yang.mapper.MarketUserMapper;
import com.yang.service.admin.AdminOrderService;
import jdk.nashorn.internal.ir.ReturnNode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: 杨庆
 * @since: 2022.9.7 15:18
 * @description:
 */
@Service
public class AdminOrderServiceImpl implements AdminOrderService {

    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;

    @Override
    public CommonData selectAll(OrderLIstBO orderLIstBO) {
        Integer page = orderLIstBO.getPage();
        Integer limit = orderLIstBO.getLimit();
        if (page==0 && limit==0){
            page=1;
            limit=20;
        }
        //TODO 分页的开始
        PageHelper.startPage(page,limit);
        MarketOrderExample example = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example.createCriteria();
        String order = orderLIstBO.getOrder();
        String sort = orderLIstBO.getSort();
        //排序
        if (!StringUtils.isEmpty(sort)){
            if (!StringUtils.isEmpty(order)){
                example.setOrderByClause(sort+" "+order);
            }else {
                example.setOrderByClause(sort);
            }
        }
        //根据id
        if (orderLIstBO.getUserId() != null) {
            criteria.andUserIdEqualTo(orderLIstBO.getUserId());
        }
        //根据订单的编号
        if (!StringUtils.isEmpty(orderLIstBO.getOrderSn())) {
            criteria.andOrderSnEqualTo(orderLIstBO.getOrderSn());
        }
        //根据日期
        String start = orderLIstBO.getStart();
        String end = orderLIstBO.getEnd();
        if (!StringUtils.isEmpty(start) && !StringUtils.isEmpty(end)) {
            try {
                Date start1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(start);
                Date end1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(end);
                criteria.andAddTimeBetween(start1,end1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //根据状态
        Integer[] orderStatusArray = orderLIstBO.getOrderStatusArray();
        if (orderStatusArray!=null){
            ArrayList<Short> list = new ArrayList<>();
            for (int integer : orderStatusArray) {
                list.add((short) integer);
            }
            criteria.andOrderStatusIn(list);
        }
        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(example);
        PageInfo<MarketOrder> pageInfo = new PageInfo<>(marketOrders);
        CommonData data = CommonData.data(pageInfo);
        return data;
    }

    @Override
    public OrderDetailVO selectById(Integer id) {
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(id);
        Integer userId = marketOrder.getUserId();
        MarketUser marketUser = marketUserMapper.selectByPrimaryKey(userId);
        UserOrder userOrder = new UserOrder();
        if(marketUser!=null){
            userOrder.setAvatar(marketUser.getAvatar());
            userOrder.setNickname(marketUser.getNickname());
        }
        Integer orderId = marketOrder.getId();
        MarketOrderGoodsExample example = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(example);
        OrderDetailVO orderDetailVO = new OrderDetailVO();
        orderDetailVO.setOrder(marketOrder);
        orderDetailVO.setOrderGoods(marketOrderGoods);
        orderDetailVO.setUser(userOrder);
        return orderDetailVO;
    }

    @Override
    public int ship(ShipBO shipBO) {

        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(shipBO.getOrderId());
        marketOrder.setShipChannel(shipBO.getShipChannel());
        marketOrder.setShipSn(shipBO.getShipSn());
        marketOrder.setOrderStatus((short) 301);

        MarketOrderExample example = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(marketOrder.getId());
        int count = marketOrderMapper.updateByExampleSelective(marketOrder,example);
        if (count == 1) {
            return 200;
        }
        return 404;
    }

    @Override
    public void delete(Integer orderId) {
        marketOrderMapper.deleteByPrimaryKey(orderId);
        //删除order_goods表中的信息
        MarketOrderGoodsExample example = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        marketOrderGoodsMapper.deleteByExample(example);
    }


    /**
    * @description: 评论的回复
    [orderReplyBO]
    * @return: void
    * @author: 杨庆
    * @time: 2022.9.12 19:43
     */
    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Override
    public BaseRespVo reply(OrderReplyBO orderReplyBO) {

        MarketComment marketComment1 = marketCommentMapper.selectByPrimaryKey(orderReplyBO.getCommentId());
        if (!StringUtils.isEmpty(marketComment1.getAdminContent())){
            return BaseRespVo.error("已回复");
        }
        //根据评论的id进行回复
        MarketComment marketComment = new MarketComment();
        marketComment.setAdminContent(orderReplyBO.getContent());
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(orderReplyBO.getCommentId());
        marketCommentMapper.updateByExampleSelective(marketComment,example);
        return BaseRespVo.ok();
    }

}
